package xyz.hzxiao.base;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: scy
 * @Date: 2022-09-05 14:20
 */
public interface AbstractBeanConfig {

    /**
     * MybatisPlus分页与插件
     *
     * @return MybatisPlus插件拦截器
     */
    @Bean
    default MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 请求日期转换LocalDateTime支持
     *
     * @return LocalDateTime转换器
     */
    @Bean
    default Converter<String, LocalDateTime> localDateTimeConverter() {
        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String param) {
                return LocalDateTime.parse(param, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
        };
    }

    /**
     * 请求日期转换LocalDate支持
     *
     * @return LocalDate转换器
     */
    @Bean
    default Converter<String, LocalDate> localDateConverter() {
        return new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String param) {
                return LocalDate.parse(param, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
        };
    }

}
